adminPanel.controller("UserAcademicController",['cacheFactory','$http','$scope','$rootScope','$q','$filter',function(cacheFactory,$http,$scope,$rootScope,$q,$filter){
	$rootScope.studentProfile=null;
	$rootScope.showGrid=false;
	$rootScope.overallGrid={};
	$rootScope.searchCriteria.recordType="ACADEMICS";
	$rootScope.selectedStudent="";
	var  thisObject=this;
	$rootScope.studentProfiles=null;
	$rootScope.recommendations={};
	$rootScope.isRecommendationFound=false;
	//paging
	$rootScope.academicPageNumber=0;
	$rootScope.recommendationPageNumber=0;
	$rootScope.strengthPageNumber=0;
	$rootScope.graphPageNumber=0;
	$rootScope.strengthData=[];
	$rootScope.showStrength=false;
	
	$rootScope.next=function(name){
		if(name==='GRID'){
			$rootScope.academicPageNumber--;
			thisObject.getDetails();
		}
		if(name==='GRAPH'){
			$rootScope.graphPageNumber--;
			thisObject.getGraphData();
		}
		if(name==='STRENGTH'){
			$rootScope.strengthPageNumber--;
			thisObject.getStrengthData();
		}
		if(name==='RECOMMENDATION'){
			$rootScope.recommendationPageNumber--;
			thisObject.getRecommendations();
		}
	}
	
	$rootScope.previous=function(name){
		if(name==='GRID'){
			$rootScope.academicPageNumber++;
			thisObject.getDetails();
		}
		if(name==='GRAPH'){
			$rootScope.graphPageNumber++;
			thisObject.getGraphData();
		}
		if(name==='STRENGTH'){
			$rootScope.strengthPageNumber++;
			thisObject.getStrengthData();
		}
		if(name==='RECOMMENDATION'){
			$rootScope.recommendationPageNumber++;
			thisObject.getRecommendations();
		}
	}
	
	//reset all paging counters
	this.resetPaging=function(){
		$rootScope.academicPageNumber=0;
		$rootScope.recommendationPageNumber=0;
		$rootScope.strengthPageNumber=0;
		$rootScope.graphPageNumber=0;
	}
	
	
	$rootScope.getTotalCount=function(quad){
		if(quad=='GRID')
			return Math.floor($rootScope.studentProfile.totalGradeInfo.length/3)+(($rootScope.studentProfile.totalGradeInfo.length%3==0)?0:1);
		if(quad=='GRAPH')
			return Math.floor($rootScope.studentProfile.totalGradeInfo.length/3)+(($rootScope.studentProfile.totalGradeInfo.length%3==0)?0:1);
		if(quad=='STRENGTH')
			return Math.floor($rootScope.studentProfile.totalGradeInfo.length/3)+(($rootScope.studentProfile.totalGradeInfo.length%3==0)?0:1);
		if(quad=='RECOMMENDATION')
			return Math.floor($rootScope.studentProfile.totalGradeInfo.length);
	}

	
	this.init=function(selectedStudentProfile,userDashboard){
		userDashboard.selectStudent(selectedStudentProfile.entityId);
		$rootScope.studentProfile=selectedStudentProfile;
	};
	
	$rootScope.notNull = function(p){
	    if (p != null){
	        return p;
	    } else{
	        return;
	    }
	};
	
	//render all four quads on the page
	this.renderGraphs=function(data){
		if(data && data.e=='STUDENT_PROFILE_CHANGED' && $rootScope.contentUrl.indexOf("user_academic")>-1){
				$rootScope.studentProfile=data.d;
				thisObject.resetPaging();
				thisObject.getDetails();
				thisObject.getGraphData();
				thisObject.getStrengthData();
				thisObject.getRecommendations();
		}
	};
	//attach handler to main screen select button
	RegisterEventHandler("StudentAcademics",thisObject.renderGraphs);
	
	this.getDetails= function(){
		//$rootScope.overallGrid=[];
		//set pageNumber
		$rootScope.searchCriteria.pageNumber=$rootScope.academicPageNumber;
		$http.post(BASE_URL+"user/handleRequest/USER_DASHBOARD_OVERALL_GRID",$rootScope.searchCriteria).success(function(data){
			if(data.status=='SUCCESS'){
				$rootScope.overallGrid=data.result[0];	
				$rootScope.showGrid=true;
			}else{
				alert(data.status +":"+data.msg);
				$rootScope.showGrid=true;
			}
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
		
	};

	this.getRecommendations= function(){
		$rootScope.searchCriteria.gradeId=$rootScope.studentProfile.totalGradeInfo[$rootScope.recommendationPageNumber].key;
		$http.post(BASE_URL+"user/handleRequest/USER_DASHBOARD_OVERALL_RECOMMENDATION",$rootScope.searchCriteria).success(function(data){
			if(data.status=='SUCCESS'){
				if(data.result && data.result.length>0){
					$rootScope.recommendations=data.result;
					$rootScope.isRecommendationFound=true;
				}
				else{
					$rootScope.isRecommendationFound=false;
				}
			}else if(data.status=='NOT_FOUND'){
				$rootScope.isRecommendationFound=false;
			}
			else{
				$rootScope.isRecommendationFound=false;
				alert(data.status +":"+data.msg);
			}
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
	};

	this.getGraphData= function(){
		$rootScope.searchCriteria.pageNumber=$rootScope.graphPageNumber;
		$http.post(BASE_URL+"user/handleRequest/USER_DASHBOARD_OVERALL_GRAPH",$rootScope.searchCriteria).success(function(data){
			if(data.status=='SUCCESS'){
				//var data = google.visualization.arrayToDataTable(data.result[0].objects);
				var json = data.object;
				var data1= new google.visualization.DataTable(json);

				var options = {
						title: 'Overall Performance',
						hAxis: {title: 'Grades', titleTextStyle: {color: 'red'}},
						vAxis: {title: 'Marks in %', titleTextStyle: {color: 'red'}},
						legend: {position: 'top', textStyle: {fontSize: 14}},
						chartArea: {width: '90%'}
				
				};
				// Instantiate and draw our chart, passing in some options.
				var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
				chart.draw(data1, options);
			}else{
				alert("Something Went wrong on server side, contact administrator");
			}
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
	};
	
	this.getStrengthData=function(){
		$rootScope.showStrength=false;
		$rootScope.strengthData=[];
		$rootScope.searchCriteria.pageNumber=$rootScope.strengthPageNumber;
		$http.post(BASE_URL+"user/handleRequest/USER_DASHBOARD_OVERALL_STRENGTH",$rootScope.searchCriteria).success(function(data){
			if(data.status=='SUCCESS'){
				if(data.result && data.result.length>0 && data.result[0].gradeList && data.result[0].gradeList.length>0){
					$rootScope.strengthData=data.result[0];
					$rootScope.showStrength=true;
				}
				else{
					$rootScope.showStrength=false;
				}
			}else if(data.status=='NOT_FOUND'){
				$rootScope.showStrength=false;
			}
			else{
				alert(data.status +":"+data.msg);
			}
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
	};
}]);